package com.asiainfo.bms.controller;


import com.asiainfo.bms.model.Book;
import com.asiainfo.bms.service.BookService;
import com.asiainfo.bms.util.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 图书表 前端控制器
 * </p>
 *
 * @author liu.hx
 * @since 2021-07-13
 */
@Api(tags = "图书管理")
@CrossOrigin
@RestController
@RequestMapping("/bms/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @ApiOperation("获取所有图书")
    @GetMapping("/")
    public R queryAll() {
        List<Book> list = bookService.list();
        return list != null && !list.isEmpty() ?
                R.ok("获取所有图书成功").setData("items", list) : R.error("获取所有图书失败");
    }

    @ApiOperation("分页获取图书")
    @GetMapping("/{page}/{limit}")
    public R queryByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("图书查询对象") Book book) {
        IPage<Book> pageParam = bookService.pageSelect(page, limit, book);
        List<Book> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取图书成功").setData("items", list).setData("total", total) : R.error("分页获取图书失败");
    }

    @ApiOperation("分页获取可借图书")
    @GetMapping("/borrow/{page}/{limit}")
    public R queryBorrowByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("图书查询对象") Book book) {
        IPage<Book> pageParam = bookService.pageSelectBorrow(page, limit, book);
        List<Book> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取可借图书成功").setData("items", list).setData("total", total) : R.error("分页获取可借图书失败");
    }

    @ApiOperation("修改一个图书")
    @PutMapping("/")
    public R update(@ApiParam("图书对象")
                    @RequestBody Book book) {
        return bookService.updateById(book) ?
                R.ok("修改图书成功") : R.error("修改图书失败");
    }

    @ApiOperation("新增一个图书")
    @PostMapping("/")
    public R insert(@ApiParam(value = "图书对象", required = true)
                    @RequestBody Book book) {
        return bookService.save(book) ?
                R.ok("新增图书成功") : R.error("新增图书失败");
    }

    @ApiOperation("根据ID删除图书")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam(value = "图书ID", required = true)
                        @PathVariable String id) {
        return bookService.removeById(id) ?
                R.ok("删除ID=" + id + "的图书成功") : R.error("删除ID=" + id + "的图书失败");
    }

    @ApiOperation("根据ID获取一个图书")
    @GetMapping("/{id}")
    public R queryById(@ApiParam(value = "图书ID", required = true)
                       @PathVariable String id) {
        Book book = bookService.getById(id);
        return book != null ?
                R.ok("获取ID=" + id + "的图书成功").setData("item", book) : R.error("获取ID=" + id + "的图书失败");
    }
}

